﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Windows;
using System.Windows.Data;

namespace Wpf学习
{
    //转换Attribute的数据的Enabled状态
    [System.Windows.Data.ValueConversion(typeof(byte[]), typeof(bool))]
    public class AttributeValueIsEnabledConverter : IMultiValueConverter
    {
        //values为数组，都为bool。
        //第一个参数为IsInherit；第二个参数为IsLocked；
        //其它参数为bool类型。
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            if (values[0] == DependencyProperty.UnsetValue || values[0] == null
                || values[1] == DependencyProperty.UnsetValue || values[1] == null)
            {
                return false;
            }

            bool IsInherit = System.Convert.ToBoolean(values[0]);
            bool IsLocked = System.Convert.ToBoolean(values[1]);
            //数据是继承
            if (IsInherit)
            {
                //锁住无法修改
                if (IsLocked)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            else
            {
                //数据不是继承,附加条件要全部满足

                bool flag = true;
                for (int index = 2; index < values.Length; index++)
                {
                    flag = flag & ((values[index] == DependencyProperty.UnsetValue || values[index] == null) ? false : System.Convert.ToBoolean(values[index]));
                }
                return flag;
            }
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }



    //转换Attribute的Locked的Enabled状态
    [System.Windows.Data.ValueConversion(typeof(byte[]), typeof(bool))]
    public class AttributeLockedIsEnabledConverter : IMultiValueConverter
    {
        //values为数组，都为bool。
        //第一个参数为ObjectData的类型，Object的所有数据；
        //其它参数为bool类型。
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            if (values[0] == DependencyProperty.UnsetValue || values[0] == null)
            {
                return false;
            }
            //Object的数据
            return false;
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    //转换Attribute的Locked的Enabled状态
    [System.Windows.Data.ValueConversion(typeof(byte[]), typeof(bool))]
    public class BoolConverter : IMultiValueConverter
    {
        //values为数组，都为bool。
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            bool flag = true;
            for (int index = 0; index < values.Length; index++)
            {
                flag = flag & ((values[index] == DependencyProperty.UnsetValue || values[index] == null) ? false : System.Convert.ToBoolean(values[index]));
            }
            return flag;
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
